package com.kyzh.core.uis

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View.OnLayoutChangeListener
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.view.ViewParent
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.kyzh.core.R
import com.umeng.socialize.utils.DeviceConfigInternal


class ScrollFloatinigButton :FloatingActionButton {
    private val TAG = "ScrollFloatinigButton"
    private var mX = 0f
    private var mY = 0f
    private var mParentWidth = 0
    private var mParentHeight = 0
    private var mScrollEnable = true
    private var mScrollLeft = 0
    private var mScrollTop = 0
    private val mRight = 0
    private var mScrollRight = 0
    private var mScrollBottom = 0
    private var hasScroll = false
    var isScroll = false
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        val ta: TypedArray = DeviceConfigInternal.context.obtainStyledAttributes(
            attrs,
            R.styleable.ScrollFloatinigButton
        )
        mScrollEnable = ta.getBoolean(R.styleable.ScrollFloatinigButton_scrollEnable, true)
        ta.recycle()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        //防止布局重置时重置ScrollFloatinigButton的位置
        val parent: ViewParent = parent
        if (parent is ViewGroup) {
            (getParent() as ViewGroup).addOnLayoutChangeListener(OnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
                if (hasScroll && mScrollRight != 0 && mScrollBottom != 0) layout(
                    mScrollLeft,
                    mScrollTop,
                    mScrollRight,
                    mScrollBottom
                )
            })
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (parent is ViewGroup) {
            mParentWidth = (parent as ViewGroup).getWidth()
            mParentHeight = (parent as ViewGroup).getHeight()
        }
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        //  这样就滑动不冲突了   客户二次开发注意   不想滑动可以用原生了  或者关闭控件滑动
        parent.parent.requestDisallowInterceptTouchEvent( true );
        if (!mScrollEnable) return super.onTouchEvent(ev)
        when (ev.getAction()) {
            MotionEvent.ACTION_DOWN -> {
                mX = ev.getX()
                mY = ev.getY()
                super.onTouchEvent(ev)
                return true
            }
            MotionEvent.ACTION_MOVE -> {
                val scaledTouchSlop: Int = ViewConfiguration.get(context).getScaledTouchSlop()
                var x: Float = ev.getX()
                var y: Float = ev.getY()
                x = x - mX
                y = y - mY
                if (Math.abs(scaledTouchSlop) < Math.abs(x) || Math.abs(scaledTouchSlop) < Math.abs(
                        y
                    )
                ) {
                    isScroll = true
                }
                if (isScroll) {
                    mScrollLeft = (getX() + x).toInt()
                    mScrollTop = (getY() + y).toInt()
                    mScrollRight = (getX() + width + x).toInt()
                    mScrollBottom = (getY() + height + y).toInt()
                    //防止滑出父界面
                    if (mScrollLeft < 0 || mScrollRight > mParentWidth) {
                        mScrollLeft = getX().toInt()
                        mScrollRight = getX().toInt() + width
                    }
                    if (mScrollTop < 0 || mScrollBottom > mParentHeight) {
                        mScrollTop = getY().toInt()
                        mScrollBottom = getY().toInt() + height
                    }
                    layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom)
                    hasScroll = true
                    return true
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> if (isScroll) {
                isScroll = false
                isPressed = false //重置点击状态
                return true
            }
        }
        return super.onTouchEvent(ev)
    }

    fun setScrollEnable(scrollEnable: Boolean) {
        mScrollEnable = scrollEnable
    }
    fun isScrollEnable(): Boolean {
        return mScrollEnable
    }
}